clear all
set mem 100m
set more off

global path "C:\Users\Felix Tintelnot\Dropbox\WindBorder\RestatCGTReplication\Data"

cd "$path\original_files"

insheet using "GeoPC_DE.csv", delimiter(";")

gen my_ind = _n
sort zip my_ind 
quietly by zip:  gen dup = cond(_N==1,0,_n)
drop if dup>1
sort zip
keep zip lat lng iso2

cd ..
cd temp_files
save german_zip_code_data, replace

clear all
cd ..
cd original_files

insheet using "german_data.csv", comma




egen project_temp = ends(plz), punct(" ") last 
egen zip = ends(plz), punct(" ") head 
egen year    = ends(seit), punct("/") last

gen project_letter = substr(project_temp,1,1) 

destring zip, replace 
merge m:1 zip using "$path\temp_files\german_zip_code_data.dta"


replace lat = 0 if _merge == 1 
replace lng = 0 if _merge == 1 

replace lat = 50.7682  if zip == 8439
replace lng = 12.36675 if zip == 8439

replace lat = 54.497852  if zip == 25875
replace lng = 8.855570 if zip == 25875


drop if _merge == 2
drop _merge

destring year, replace

replace year = 1982 if year == 82
replace year = 1983 if year == 83
replace year = 1984 if year == 84
replace year = 1985 if year == 85
replace year = 1986 if year == 86
replace year = 1987 if year == 87
replace year = 1988 if year == 88
replace year = 1989 if year == 89
replace year = 1990 if year == 90
replace year = 1991 if year == 91
replace year = 1992 if year == 92
replace year = 1993 if year == 93
replace year = 1994 if year == 94
replace year = 1995 if year == 95
replace year = 1996 if year == 96
replace year = 1997 if year == 97
replace year = 1998 if year == 98
replace year = 1999 if year == 99
replace year = 2000 if year == 0
replace year = 2001 if year == 1
replace year = 2002 if year == 2
replace year = 2003 if year == 3
replace year = 2004 if year == 4
replace year = 2005 if year == 5


keep if year >= 1995 

gen n = 1

replace herstl = "Bonus" if herstl == "AN BONUS"
replace herstl = "DeWind" if herstl == "Dewind"
replace herstl = "Suedwind" if herstl == "S�DWIND"
replace herstl = "Fuhrlaender" if herstl == "Fuhrl�nder"


rename herstl prod
rename zip zip_code
rename nabhoe hub
rename durchm rotor


collapse (sum) n (mean) hub rotor lng lat year, by (prod zip_code project_letter kw iso2)

replace year = round(year)

drop if prod =="Autoflug"
drop if prod =="Wenus"

gen germany = 1
gen denmark = 0
gen type = "LAND"
gen total_mw = kw * n /1000



/* Distance to border crossings */

gen border_lat1 = 54.80558257
gen border_lat2 = 54.83985
gen border_lat3 = 54.872063
gen border_lat4 = 54.906225
gen border_lat5 = 54.914909

gen border_lng1 = 9.328680038
gen border_lng2 = 9.404297
gen border_lng3 = 9.077797
gen border_lng4 = 8.911972
gen border_lng5 = 8.671646

geodist lat lng border_lat1 border_lng1, generate(distance_b1) sphere radius(6370.973279)
geodist lat lng border_lat2 border_lng2, generate(distance_b2) sphere radius(6370.973279)
geodist lat lng border_lat3 border_lng3, generate(distance_b3) sphere radius(6370.973279)
geodist lat lng border_lat4 border_lng4, generate(distance_b4) sphere radius(6370.973279)
geodist lat lng border_lat5 border_lng5, generate(distance_b5) sphere radius(6370.973279)

gen distance_border = min(distance_b1,distance_b2,distance_b3,distance_b4,distance_b5)
drop border_lat1 border_lat2 border_lat3 border_lat4 border_lat5 border_lng1 border_lng2 border_lng3 border_lng4 border_lng5 distance_b1 distance_b2 distance_b3 distance_b4 distance_b5


/* Distance to producers */

gen Bonus_lat       = 55.9551
gen Bonus_lng       = 9.12447
gen Enercon_lat  	= 53.4692
gen Enercon_lng	    = 7.46116
gen Fuhrlaender_lat	= 50.704
gen Fuhrlaender_lng	= 8.0811
gen Windworld_lat	= 57.059
gen Windworld_lng	= 9.93439
gen Micon_lat	    = 56.4285
gen Micon_lng	    = 10.0394
gen Nordex_lat	    = 54.0738
gen Nordex_lng	    = 12.1313
gen Nordtank_lat	= 56.3203
gen Nordtank_lng	= 10.7913
gen Suedwind_lat	= 52.5027
gen Suedwind_lng	= 13.4017
gen Tacke_lat	    = 52.3319	
gen Tacke_lng	    = 7.42311
gen Vestas_lat	    = 56.0239
gen Vestas_lng      = 8.38205

																		

foreach name in "Bonus" "Nordtank" "Micon" "Windworld" "Vestas" "Nordex" "Enercon" "Fuhrlaender" "Suedwind" "Tacke"{ 
geodist lat lng `name'_lat `name'_lng, generate(distance_`name') sphere radius(6370.973279)
}

drop Bonus_lat	Bonus_lng	Enercon_lat	Enercon_lng	Fuhrlaender_lat	Fuhrlaender_lng	Windworld_lat	Windworld_lng	Micon_lat	Micon_lng	Nordex_lat	Nordex_lng	Nordtank_lat	Nordtank_lng	Suedwind_lat	Suedwind_lng	Tacke_lat	Tacke_lng	Vestas_lat	Vestas_lng


keep if year == 1995 | year == 1996
replace prod = "Fringe" if (prod !="Bonus" & prod !="Nordex" & prod !="Enercon" & prod !="Vestas" & prod !="Wind World" & prod !="Micon"  & prod !="Nordtank" & prod !="Fuhrlaender" & prod !="Suedwind" & prod !="Tacke")


/* Bring in Road Distances*/

sort prod kw zip_code project_letter year
merge 1:1 prod kw zip_code project_letter year using "$path\additional_files\German_project_id"
drop _merge

sort project_id

save "$path\temp_files\german_data_olddistances", replace
clear all

insheet using "$path\additional_files\ger_road_distance.csv", names

replace distance_bonus_road     = distance_bonus_road / 1000
replace distance_nordtank_road  = distance_nordtank_road / 1000
replace distance_micon_road     = distance_micon_road / 1000
replace distance_vestas_road    = distance_vestas_road / 1000
replace distance_windworld_road = distance_windworld_road / 1000
replace distance_nordex_road    = distance_nordex_road / 1000
replace distance_enercon_road   = distance_enercon_road / 1000
replace distance_fuhrlaender_road = distance_fuhrlaender_road / 1000
replace distance_suedwind_road  = distance_suedwind_road / 1000
replace distance_tacke_road     = distance_tacke_road / 1000

merge 1:1 project_id using "$path\temp_files\german_data_olddistances.dta"
erase "$path\temp_files\german_data_olddistances.dta"
drop _merge


// There is one project on a ferry-less island for which there is no road distance from ANY
// manufacturer to the plant. This line eliminates that project (project_id==877). 
drop if distance_bonus_road < 0



gen bonus = 1 if prod =="Bonus"
replace bonus = 0 if bonus == . 

gen vestas = 1 if prod =="Vestas"
replace vestas = 0 if vestas == . 

gen nordex = 1 if prod =="Nordex"
replace nordex = 0 if nordex == .

gen enercon = 1 if prod =="Enercon"
replace enercon = 0 if enercon == .

gen windworld = 1 if prod =="Wind World"
replace windworld = 0 if windworld == . 

gen micon = 1 if prod =="Micon"
replace micon = 0 if micon == .  

gen nordtank = 1 if prod =="Nordtank"
replace nordtank = 0 if nordtank == . 

gen fuhrlaender = 1 if prod =="Fuhrlaender"
replace fuhrlaender = 0 if fuhrlaender == .

gen suedwind = 1 if prod =="Suedwind"
replace suedwind = 0 if suedwind == .

gen tacke = 1 if prod =="Tacke"
replace tacke = 0 if tacke == .   

gen fringe = 1 if prod =="Fringe"
replace fringe = 0 if fringe == . 


/* Construct state border dummies */

gen Enercon_s = "DE-NI"
gen Tacke_s = "DE-NI"
gen Nordex_s = "DE-MV"
gen Suedwind_s = "DE-BE"
gen Fuhrlaender_s = "DE-RP"


foreach name in "Nordex" "Enercon" "Fuhrlaender" "Suedwind" "Tacke"{ 
gen `name'_d = 1
replace `name'_d = 0 if iso2 == `name'_s
}



/* If Road Distances are used */
order kw total_mw n fringe bonus nordtank micon vestas windworld nordex enercon fuhrlaender suedwind tacke distance_bonus_road distance_nordtank_road distance_micon_road distance_vestas_road distance_windworld_road distance_nordex_road distance_enercon_road distance_fuhrlaender_road distance_suedwind_road distance_tacke_road Nordex_d Enercon_d Fuhrlaender_d Suedwind_d Tacke_d distance_border
outsheet kw total_mw n fringe bonus nordtank micon vestas windworld nordex enercon fuhrlaender suedwind tacke distance_bonus_road distance_nordtank_road distance_micon_road distance_vestas_road distance_windworld_road distance_nordex_road distance_enercon_road distance_fuhrlaender_road distance_suedwind_road distance_tacke_road Nordex_d Enercon_d Fuhrlaender_d Suedwind_d Tacke_d distance_border using "$path\output_files\german_data_for_matlab_new", comma replace

/* If Great circle distances are used */
order kw total_mw n fringe bonus nordtank micon vestas windworld nordex enercon fuhrlaender suedwind tacke distance_Bonus distance_Nordtank distance_Micon distance_Vestas distance_Windworld distance_Nordex distance_Enercon distance_Fuhrlaender distance_Suedwind distance_Tacke Nordex_d Enercon_d Fuhrlaender_d Suedwind_d Tacke_d distance_border
outsheet kw total_mw n fringe bonus nordtank micon vestas windworld nordex enercon fuhrlaender suedwind tacke distance_Bonus distance_Nordtank distance_Micon distance_Vestas distance_Windworld distance_Nordex distance_Enercon distance_Fuhrlaender distance_Suedwind distance_Tacke Nordex_d Enercon_d Fuhrlaender_d Suedwind_d Tacke_d distance_border using "$path\output_files\german_data_for_matlab", comma replace


/* Bring in producer nationality */

merge m:1 prod using "$path\original_files\producer_country_denmark.dta" 


drop if _merge ==2
drop _merge
replace prodc_denmark = 0 if prod == "Fringe"

rename lng longitude
rename lat latitude



saveold "$path\output_files\german_data_9596", replace

